/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package lib;

import java.sql.*;

/**
 *
 * @author felipe
 */
public class DB implements IFDB{
    
    public String host = "localhost";
    public String user = "root";
    public String pass = "";
    public String db = "cinema";
    //public int port;

    protected static DB instance = null;

    protected String url = "";
    protected Connection driver = null;
    protected Statement stmt = null;
    protected ResultSet rs = null;
    protected String classDrive = "";

    public DB()
    {
        this.url = "jdbc:mysql://" + this.host + ":3306/" + this.db;
        this.classDrive = "com.mysql.jdbc.Driver";
        try
        {
            Class.forName(this.classDrive);
        }
        catch(Exception e)
        {
            ErrorLog.JavaLog("Error import drive " + this.classDrive , e);
        }
        this.Open();
    }

    public static DB Singleton()
    {
        if (instance == null )
        {
            instance = new DB();
        }
        return instance;
    }

    public boolean Open(){
        try
        {
            this.driver = DriverManager.getConnection(this.url, this.user, this.pass);
            if( this.driver.isClosed())
            {
                return false;
            }
        }
        catch(SQLException e)
        {
            ErrorLog.SQLLog("Error Open method ", e);
        }

        return true;
    }

    public ResultSet Execute(String sql){
        try
        {
            this.stmt = this.driver.createStatement();
            this.rs = this.stmt.executeQuery(sql);
        }
        catch(SQLException e)
        {
            ErrorLog.SQLLog("Error Execute method " , e);
        }
        return this.rs;
    }

    public int Update(String sql){
        int up = 0;
        try
        {
            this.stmt = this.driver.createStatement();
            up = this.stmt.executeUpdate(sql);
        }
        catch(SQLException e)
        {
             ErrorLog.SQLLog("Error Update method " , e);
        }
        return up;
    }

    public void AutoCommit(boolean commit)
    {
        try
        {
            if ( ! this.driver.getAutoCommit() )
            {
                this.driver.setAutoCommit(commit);
            }
        }
        catch(SQLException e)
        {
            ErrorLog.SQLLog("Error AutoCommit method " , e);
        }
    }

    public void Commit()
    {
        try
        {
            this.driver.commit();
        }
        catch(SQLException e)
        {
            ErrorLog.SQLLog("Error AutoCommit method " , e);
        }
    }

    public void RollBack()
    {
         try
        {
            this.driver.rollback();
        }
        catch(SQLException e)
        {
            ErrorLog.SQLLog("Error RollBack method " , e);
        }
    }

    public void Close()
    {
        try
        {
            if( ! this.driver.isClosed() )
            {
                this.driver.close();
            }
        }
        catch(SQLException e)
        {
            ErrorLog.SQLLog("Error Close method " , e);
        }
    }

}
